<?php

include("config.php");
if (!isset($_SESSION["user"]) || $_SESSION["user"] == "") header('Location: index.php');


$db_www = new DB($server_username,$server_pass,$server_db,$server,$driver);
$db_rdata = new DB($rdata_server_username,$rdata_server_pass,$rdata_server_db,$rdata_server,$rdata_driver);
if ($db_www->isConnected()) {	
	$i_query = str_replace("<kodas>", $_SESSION['user'], $i_query);
	$minfo = $db_www->returnInfo($i_query);	
} else {
	die("Klientų aptarnavimo sistema laikinai neveikia.");
}

if (!$db_rdata->isConnected()) {	
	die("Klientų aptarnavimo sistema laikinai neveikia(rdata).");
}

	if (!empty($minfo)) {
		$card_query_nr = str_replace("<im_kodas>", $minfo["N08_kodas_ks"], $card_query_nr);
		$card_query_nr = str_replace("<kodas>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["code"])), $card_query_nr);
		$cinfo = $db_www->returnInfo($card_query_nr);			
	} else {
		die("Nerasta kortelė.");
	}

		if (isset($_REQUEST["from"])) {
			$from = convertdate($_REQUEST["from"],'iso','iso','.');
			if ($from == NULL) $from = firstOfMonth();
		} else {
			$from = firstOfMonth();
		}
		if (isset($_REQUEST["to"])) {
			$to = convertdate($_REQUEST["to"],'iso','iso','.');
			if ($to == NULL) $to = lastOfMonth();
		} else {
			$to = lastOfMonth();
		}	
		
		$sells_query = str_replace("<kodas>", @iconv('UTF-8','windows-1257',addslashes_mssql($_REQUEST["code"])), $sells_query);
		//$sells_query = str_replace("<kodas>", "LAVINSKI9", $sells_query);
		$sells_query = str_replace("<from>", str_replace(".","-",$from), $sells_query);
		$sells_query = str_replace("<to>", str_replace(".","-",$to), $sells_query);
		$info = $db_rdata->returnList($sells_query);
        //echo $sells_query; 


	
if (isset($_REQUEST["print"])){


	$oinfo = $db_www->returnInfo($o_query);	
	if (!empty($oinfo)) {
		$o_query = str_replace("<kodas>", $oinfo["N23_REIKSME1"], $ii_query);
		$ourinfo = $db_www->returnInfo($o_query);	
		
		$s_query = str_replace("<kodas>", $oinfo["N23_REIKSME1"], $s_query);
		$saskinfo = $db_www->returnInfo($s_query);
	}

	$i_query = str_replace("<kodas>", $_SESSION['user'], $i_query);
	//echo $i_query;
	$minfo = $db_www->returnInfo($i_query);	
	
	
		require_once('tcpdf/config/lang/eng.php');
		require_once('tcpdf/tcpdf.php');

		// create new PDF document
		$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

		// set document information
		$pdf->SetCreator(PDF_CREATOR);
		$pdf->SetAuthor('Darius Mikėnas');
		$pdf->SetTitle('Kortelės ataskaita');
		$pdf->SetSubject('Ataskaita');
		$pdf->SetKeywords('Ataskaita');

		$pdf->setPrintHeader(false);
		$pdf->setPrintFooter(false);

		// set default monospaced font
		$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

		//set margins
		$pdf->SetMargins(7, 7, 7);
		$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
		$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

		//set auto page breaks
		$pdf->SetAutoPageBreak(TRUE, 10);

		//set image scale factor
		$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

		//set some language-dependent strings
		$pdf->setLanguageArray($l);

		// ---------------------------------------------------------

		// set font
		$pdf->SetFont('freesans', '', 8);

		$pdf->setJPEGQuality(75);
		
		// add a page
		$pdf->AddPage();

		// get esternal file content
		$company = iconv('windows-1257', 'UTF-8',"<br>". $ourinfo["N08_PAV"]);
		$address = iconv('windows-1257', 'UTF-8',"<br>".$ourinfo["N08_ADR"]);
		
		$mcompany = iconv('windows-1257', 'UTF-8',"<br>". $minfo["N08_PAV"]);
		$maddress = iconv('windows-1257', 'UTF-8',"<br>".$minfo["N08_ADR"]);


$viso = 0;
$visonuol = 0;
$visokiekis = 0;
$data = "";
$visobepvm1 = 0;
	foreach ($info as $card) {
	
		$p_query = str_replace("<barkodas>", $card["Barkodas"] , $prek_query);
		$pinfo = $db_www->returnInfo($p_query);
		if (!empty($pinfo)) $pav = trim($pinfo["n37_pav"]);
		else $pav = "";
		if (!empty($pinfo)) $vnt = trim($pinfo["n37_kodas_us"]);
		else $vnt = "";
	
		if ($card["nuolvienet"] != 0) {
			$nuolvien = trim(sprintf("%01.3f",$card["nuolvienet"]));
		} else {
			$nuolvien = "";
		}
		if ($card["Kiekis"] != null && $card["Kiekis"] != 0) {
			$suma = sprintf("%01.4f",($card["Suma"]-$card["Mokesciai"])/$card["Kiekis"]);
			$visobepvm = sprintf("%01.4f",($card["Suma"]-$card["Mokesciai"]));
			$sumabepvm = sprintf("%01.3f",$card["Suma"]/$card["Kiekis"]);
		} else {
			$suma = "0";
			$visobepvm = "0";
			$sumabepvm = "0";
		}
	
		$data .= "<tr>";
		$data .= "<td style=\"border-bottom:1px solid #EFF9E5;\"><font size=\"-2\">".$card["Year"] . "-" . sprintf('%02d',$card["Month"]) . "-" . sprintf('%02d',$card["Day"]) . " " . sprintf('%02d',$card["Hour"]) . ":" . sprintf('%02d',$card["Minute"])."</font></td>";
		$data .= "<td style=\"border-bottom:1px solid #EFF9E5;\"><font size=\"-2\">".trim(iconv('windows-1257','UTF-8',$card["AparatoPavadinimas"]))."</font></td>";
		$data .= "<td style=\"border-bottom:1px solid #EFF9E5;\"><font size=\"-2\">".trim($card["KvitoNr2"])."</font></td>";
		$data .= "<td style=\"border-bottom:1px solid #EFF9E5;\"><font size=\"-2\">".trim($card["Barkodas"])."</font></td>";
		$data .= "<td style=\"border-bottom:1px solid #EFF9E5;\"><font size=\"-2\">".iconv('windows-1257', 'UTF-8',htmlspecialchars($pav))."</font></td>";
		$data .= "<td style=\"border-bottom:1px solid #EFF9E5;\"><font size=\"-2\">".trim(sprintf("%01.4f",$card["Kiekis"]))."</font></td>";
		$data .= "<td style=\"border-bottom:1px solid #EFF9E5;\"><font size=\"-2\">".iconv('windows-1257', 'UTF-8',$vnt)."</font></td>";
		$data .= "<td style=\"border-bottom:1px solid #EFF9E5;\"><font size=\"-2\">".$nuolvien."</font></td>";
		$data .= "<td style=\"border-bottom:1px solid #EFF9E5;\"><font size=\"-2\">".trim(sprintf("%01.3f",$card["nuolaida"]))."</font></td>";
		$data .= "<td style=\"border-bottom:1px solid #EFF9E5;\"><font size=\"-2\">".$suma."</font></td>";
		$data .= "<td style=\"border-bottom:1px solid #EFF9E5;\"><font size=\"-2\">".$sumabepvm."</font></td>";
		$data .= "<td style=\"border-bottom:1px solid #EFF9E5;\"><font size=\"-2\">".$visobepvm."</font></td>";
		$data .= "<td style=\"border-bottom:1px solid #EFF9E5;\"><font size=\"-2\">".sprintf("%01.2f",$card["Suma"])."</font></td>";
		$data .= "</tr>";
		
		$viso += $card["Suma"];
		$visonuol += $card["nuolaida"];
		$visokiekis += $card["Kiekis"];
		$visobepvm1 += $card["Suma"]-$card["Mokesciai"];
	}
	if ($viso > 0) {
	
		$data .= "<tr>";
		$data .= "<td colspan=\"4\"></td>";		
		$data .= "<td align=\"right\"><font size=\"-2\">Viso</font></td>";
		$data .= "<td colspan=\"3\"><font size=\"-2\">".$visokiekis."</font></td>";
		$data .= "<td colspan=\"3\"><font size=\"-2\">".sprintf("%01.3f",$visonuol)."</font></td>";
		$data .= "<td ><font size=\"-2\">".sprintf("%01.4f",$visobepvm1)."</font></td>";		
		$data .= "<td ><font size=\"-2\">".sprintf("%01.2f",$viso)."</font></td>";
		$data .= "</tr>";
	}
		
		$html = "
<table cellspacing=\"0\" cellpadding=\"1\" border=\"0\">
    <tr>
        <td><font size=\"+1\">TIEKĖJAS (PARDAVĖJAS)</font>".
		$company.
	    $address.		
		"<br>
		<table cellspacing=\"0\" cellpadding=\"1\" border=\"0\" width=\"400\">
		<tr>
		<td width=\"150\">Įmonės kodas</td><td>".$ourinfo["N08_IM_KODAS"]."</td>
		</tr>
		<tr>
		<td>PVM kodas</td><td>".$ourinfo["N08_PVM_KODAS"]."</td>
		</tr>
		<tr>
		<td>Banko kodas</td><td>".iconv('windows-1257', 'UTF-8', $saskinfo["N33_KODAS_WS"] . " " . $saskinfo["N34_PAV"])."</td>
		</tr>
		<tr>
		<td>A.s.</td><td>".$saskinfo["N33_S_KODAS"]."</td>
		</tr>		
		</table><br><br>
		
		<font size=\"+1\">PIRKĖJAS (MOKĖTOJAS)</font>".
		$mcompany.
	    $maddress.	
		"<br>
		<table cellspacing=\"0\" cellpadding=\"1\" border=\"0\" width=\"400\">
		<tr>
		<td width=\"150\">Įmonės kodas</td><td>".$minfo["N08_IM_KODAS"]."</td>
		</tr>
		<tr>
		<td>PVM kodas</td><td>".$minfo["N08_PVM_KODAS"]."</td>
		</tr>
	
		</table>
		</td>
        <td align=\"right\"><img src=\"logo.jpg\" alt=\"test alt attribute\" height=\"100\" border=\"0\" /></td>
    </tr>
</table>	
<br>
<span style=\"text-align:center;\"><font size=\"+1\">PARDAVIMŲ ATASKAITA</font></span>	
<br><br>Periodas " . $from . "-" . $to . "	
<br>Kortelė " . trim($cinfo["N51_PAV"]) . "        ". iconv('windows-1257', 'UTF-8',trim($cinfo["N51_KORTA"])) ."<br><br>

	<table cellspacing=\"0\" cellpadding=\"1\" border = \"0\">
					<tr style=\"background-color:#EFF9E5;\"><th style=\"width:75px;\"><font size=\"-2\">Data, laikas</font></th><th style=\"width:90px;\"><font size=\"-2\">Vieta</font></th><th style=\"width:45px;\"><font size=\"-2\">Kvito nr.</font></th><th style=\"width:60px;\"><font size=\"-2\">Barkodas</font></th><th style=\"width:92px;\"><font size=\"-2\" >Prekės pavadinimas</font></th><th style=\"width:45px;\"><font size=\"-2\">Kiekis</font></th><th  style=\"width:23px;\"><font size=\"-2\">Mato<br>vnt.</font></th><th  style=\"width:40px;\"><font size=\"-2\">Nuolaida<br>vienetui</font></th><th  style=\"width:35px;\"><font size=\"-2\">Nuolaid.<br>suma</font></th><th  style=\"width:44px;\"><font size=\"-2\">Vnt. kaina be PVM</font></th><th  style=\"width:45px;\"><font size=\"-2\">Vnt. kaina su PVM</font></th><th  style=\"width:50px;\"><font size=\"-2\">Suma viso be PVM</font></th><th style=\"width:50px;\"><font size=\"-2\">Suma viso</font></th></tr>
	"
	
	.$data.
	
	"</table>
		";

		$pdf->writeHTML($html, true, false, true, false, '');

	
		
		// ---------------------------------------------------------



		//Close and output PDF document
		$pdf->Output('ataskaita.pdf', 'D');

} else {	

	include("header.php");
	include("menu.php");

	?>
	<table class="innercont" cellspacing="0" cellpadding="5">
<?php
if (isset($oldstyle) && $oldstyle == "1") {
?>
	<tr><td class="title">Kortelės informacija</td></tr>
	<tr><td>
<?php
		if (!empty($cinfo)) {			
	?>	
	<table cellspacing="0" cellpadding="5" border = "0" width="650">
	<?php

		echo "<tr><td></td><td>".trim($cinfo["N51_PAV"])."</td><td width='450' align='left'>".@iconv('windows-1257', 'UTF-8', htmlspecialchars($cinfo["N51_KORTA"]))."</td></tr>\n";
		echo "<tr><td></td><td colspan='2'>";
		?>
		<br>
		<form method="post" action="card.php?code=<?php echo $_REQUEST["code"];?>">	
		<table cellspacing="0" cellpadding="1" border = "0">
		<tr><td align="right">Periodas nuo</td><td><input  class="dt" type='text' name="from" value="<?php echo $from;?>"></td><td valign="bottom" rowspan="2"><input type = "submit" value = "Nuskaityti"></td></tr>
		<tr><td align="right">iki</td><td><input  class="dt" type='text' name="to" value="<?php echo $to;?>"></td></tr>
		</table>
		</form>
		<?php
		echo "</td></tr>"; 
	?>
	</table>	
<?php
	}
} else {
	echo "<tr><td class='leftcol'><br>";
	echo "<b>Kortelės informacija</b><br>";
	echo "Nr. " . trim($cinfo["N51_PAV"]) . "<br>";
	echo @iconv('windows-1257', 'UTF-8', htmlspecialchars($cinfo["N51_KORTA"])) . "<br><br>";
?>
  <script>
  $(function() {
    $( ".dt" ).datepicker({
      showOn: "button",
      buttonImage: "img/calendar.gif",
      buttonImageOnly: true,
	  dateFormat: "yy.mm.dd",
	  regional: "lt"
    });
  });
  </script>
  <?php	

	echo "<form method=\"post\" action='card.php?code=".$_REQUEST["code"]."'><table cellspacing='0' cellpadding='5' class='params'>";
	echo "<tr><th style='text-align:left;'>Pasirinkite periodą:</th></tr>";
	echo "<tr><td><table border='0' cellspacing='0' cellpadding='0'><tr><td style='padding:0;text-align:left;width:40px;'>Nuo</td><td style='padding:0;text-align:right;'><input class=\"dt\" type='text' name=\"from\" value=\"" . $from ."\"></td></tr>";
	echo "<tr><td style='padding:0;text-align:left;width:40px;'>Iki</td><td style='padding:0;text-align:right;'><input class=\"dt\" type='text' name=\"to\" value=\"" . $to ."\"></td></tr>";
	echo "</table></td></tr>";
	
	echo "<tr><td style='text-align:left;'><table border='0' cellspacing='0' cellpadding='0'><tr><td style='width:80px;padding:0px;text-align:left;'><button class='but bgfilter nopad' type = \"submit\">Nuskaityti</button></td>";
	echo "<td style='padding:0;text-align:right;width:50px;'><button class='but bgfilter nopad' onClick=\"javascript:location.href='cards.php';\" type='button' value='Atgal'>Atgal</button>";
	echo "</td></tr></table></td></tr>";
	echo "</table></form>";	
	
	
	
	echo "</td><td class='rightcol'><br>";
}
?>
	

	<?php
		if (!empty($cinfo)) {			
	?>
	<table class="innercont4" cellspacing="0" cellpadding="0">
	<?php
	if (!empty($info)) {
	?>
	<tr><th>Data, laikas</th><th>Vieta</th><th>Kvito nr.</th><th>Barkodas</th><th>Prekės pavadinimas</th><th>Kiekis</th><th>Mato VNT</th><th>Nuolaida vienetui</th><th>Nuol. suma</th><th nowrap>Vnt. kaina<br>be PVM</th><th nowrap>Vnt. kaina<br>su PVM</th><th nowrap>Suma viso<br>be PVM</th><th>Suma viso</th></tr>
	<?php
	}
	$viso = 0;
	$visonuol = 0;
	$visokiekis = 0;
	$visobepvm1 = 0;
	foreach ($info as $card) {
	
		$p_query = str_replace("<barkodas>", $card["Barkodas"] , $prek_query);
		$pinfo = $db_www->returnInfo($p_query);		
		//echo $prek_query;

		if ($card["nuolvienet"] != 0) {
			$nuolvien = trim(sprintf("%01.3f",$card["nuolvienet"]));
		} else {
			$nuolvien = "";
		}
		if ($card["Kiekis"] != null && $card["Kiekis"] != 0) {
			$suma = sprintf("%01.4f",($card["Suma"]-$card["Mokesciai"])/$card["Kiekis"]);
			$visobepvm = sprintf("%01.4f",($card["Suma"]-$card["Mokesciai"]));
			$sumabepvm = sprintf("%01.3f",$card["Suma"]/$card["Kiekis"]);
		} else {
			$suma = "0";
			$visobepvm = "0";
			$sumabepvm = "0";
		}		
	?>
	<tr>
	<td nowrap><?php echo $card["Year"] . "-" . sprintf('%02d',$card["Month"]) . "-" . sprintf('%02d',$card["Day"]) . " " . sprintf('%02d',$card["Hour"]) . ":" . sprintf('%02d',$card["Minute"]);?></td>
	<td><?php echo iconv('windows-1257', 'UTF-8',$card["AparatoPavadinimas"]);?></td>
	<td><?php echo $card["KvitoNr2"];?></td>
	<td><?php echo $card["Barkodas"];?></td>
	<td><?php if (!empty($pinfo)) echo iconv('windows-1257', 'UTF-8',$pinfo["n37_pav"]);?></td>
	<td><?php echo sprintf("%01.4f",$card["Kiekis"]);?></td>
	<td><?php if (!empty($pinfo)) echo iconv('windows-1257', 'UTF-8',$pinfo["n37_kodas_us"]);?></td>
	<td><?php echo $nuolvien;?></td>
	<td><?php echo sprintf("%01.3f",$card["nuolaida"]);?></td>
	<td><?php echo $suma;?></td>	
	<td><?php echo $sumabepvm;?></td>
	<td><?php echo $visobepvm;?></td>
	<td><?php echo sprintf("%01.2f",$card["Suma"]);?></td>
	</tr>
	<?php
	$viso += $card["Suma"];
	$visonuol += $card["nuolaida"];
	$visokiekis += $card["Kiekis"];
	$visobepvm1 += $card["Suma"]-$card["Mokesciai"];
	}
	if ($viso > 0) {
	?>
	<tr>
	<td colspan="4" class="viso"></td>
	<td align="right" class="viso">Viso</td>
	<td class="viso" colspan="3"><?php echo sprintf("%01.4f",$visokiekis);?></td>
	<td class="viso" colspan="3"><?php echo sprintf("%01.3f",$visonuol);?></td>
	<td class="viso"><?php echo sprintf("%01.4f",$visobepvm1);?></td>
	<td class="viso"><?php echo $viso;?></td>
	</tr>
	<?php
	}
	?>
	<tr>
	<td colspan="9">
	<?php
	if ($viso > 0) {
	?>
	<input class="but bgwhite" type=button onClick="javascript:location.href='card.php?print=1&code=<?php echo $_REQUEST["code"];?>&from=<?php echo $from;?>&to=<?php echo $to;?>;'" value='Spausdinti'>
	<?php
	}
	?>
	</td>
	<td colspan="2" align="right">
	<?php
	if (isset($oldstyle) && $oldstyle == "1") {
	?>
	<input type=button onClick="javascript:location.href='cards.php'" value='Atgal'>
	<?php
	}
	?>
	</td></tr>
	</table>
			
	<?php
		} else {
			echo "Nerasta kortelė";
		}
	?>
	</td></tr>
	</table>

	<?php
	include("footer.php");
}
?>